package hrms.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import hrms.dao.AdminDao;
import hrms.dao.RewardDao;
import hrms.entity.Reward;
import hrms.util.PageUtil;
import hrms.util.Result;

@Service("rewardService")
public class RewardServiceImpl implements RewardService{
	
	@Resource(name="adminDao")
	private AdminDao adminDao ;
	
	@Resource(name="rewardDao")
	private RewardDao dao;

	
	public Result<List<Reward>> findByPage(String pages ,HttpServletResponse res) {
		String message = "Reward";
		return new PageUtil<Reward>().findByPage(pages, res, dao, message);
	}
	
	
	public Result<Map<String, Integer>> pageTotal(HttpServletRequest req){
		String message = "Reward";
		return PageUtil.pageTotal(req, message); 
	}
	
	
	public Result<Object> addReward(String type, String name, String detail, String deal, String date) {
		Result<Object> result = new Result<Object>();
		
		Reward e = new Reward();
		e.setrEmpName(name);
		e.setrDeal(deal);
		e.setrDetail(detail);
		e.setrType(type);
		e.setrDate(date);
		int num = dao.saveReward(e);
		if(num!=1){
			result.setStatus(1);
			result.setMsg("创建出错！");
		} else {
			result.setStatus(0);
			result.setMsg("创建成功！");
		}
		return result;
	}


	public Result<List<Reward>> search(String name) {
		Result<List<Reward>> result = new Result<List<Reward>>();
	    List<Reward> list = dao.findSearch("%"+name+"%");
		result.setData(list);
		result.setStatus(0);
		return result;
	}


	/*
	public Result<Object> changeState(String RewardId, HttpServletRequest req) {
		Result<Object> result = new Result<Object>();
		int num = dao.changeState(RewardId);
		
		Cookie[] c = req.getCookies();
		String page = "1";
		for(Cookie cc : c){
			if(cc.getName().equals("RewardPage")){
				page = cc.getValue();
			}
		}
		int pageNum = Integer.parseInt(page);
		if(num != 1){
			result.setStatus(1);
			result.setMsg("修改出错");
		} else {
			result.setStatus(0);
			result.setMsg("修改成功");
			result.setData(pageNum);
		}
		return result;
	}

	/*
	public Result<Reward> findReward(String RewardId, HttpServletRequest req) {
		Reward e = dao.findByRewardId(Integer.parseInt(RewardId));
		Result<Reward> r = new Result<Reward>();
		if(e != null){
			r.setData(e);
			r.setMsg("");
			r.setStatus(0);
			return r;
		}
		else{
			r.setStatus(1);
			r.setMsg("未找到员工信息！");
			return null;
		}
		
		
	}

	public Result<Object> uppdateReward(String RewardId, String RewardName, String RewardSex, String RewardAge, String RewardJob,
			String RewardDept,HttpServletRequest req) {
			Result<Object> result = new Result<Object>();
			//获取当前登录的用户
			Admin adminUser = (Admin)req.getSession().getAttribute("admin");
			Integer rollUser = adminUser.getRoll();
			//获取将要被删除的部门号
			
			if(rollUser == 2 ){
				result.setStatus(2);
				result.setMsg("权限不够!");
				return result;
			}
			dao.deleteReward(Integer.parseInt(RewardId));
			Reward e = new Reward();
			e.setRewardId(Integer.parseInt(RewardId));
			e.setAge(Integer.parseInt(RewardAge));
			e.setRewardName(RewardName);
			e.setGender(RewardSex);
			e.setHiredate(new Date(System.currentTimeMillis()));
			String deptId = dao.findDeptIdByName(RewardDept);
			if(deptId == null){
				result.setStatus(3);
				result.setMsg("部门不存在！");
				return result;
			}
			e.setdName(RewardDept);
			e.setDeptId(Integer.parseInt(deptId));
			e.setJob(RewardJob);
			
			int num = dao.updateReward(e);
			if(num!=1){
				result.setStatus(1);
				result.setMsg("修改出错！");
			} else {
				result.setStatus(0);
				result.setMsg("修改成功！");
			}
		return result;
	}
	*/
}